App.directive('edDeptTree', ['Page', function (Page) {
    return {

        restrict: 'A',
        scope: true,
        template: '<ul class="ztree"></ul>',

        controller: ['$scope', '$element', function ($scope, $element) {
            $scope.treeId = Metronic.getUniqueID();
            $($element).find('.ztree').attr('id', $scope.treeId);
        }],

        link: function (scope, elem, attrs) {
            var setting = {
                check: {
                    enable: false
                },
                view: {
                    selectedMulti: false
                },
                data: {
                    simpleData: {
                        enable: true,
                        idKey: "deptId",
                        pIdKey: "parentDeptId"
                    },
                    key: {
                        name: "deptName"
                    }
                },
                callback: {
                    onClick: function(event, treeId, treeNode, clickFlag) {
                        if (attrs.edClick) {
                            scope.$apply(function() {
                                scope[attrs.edClick]({
                                    deptId: treeNode.deptId,
                                    parentDeptId: treeNode.parentDeptId,
                                    deptCode: treeNode.deptCode,
                                    deptName: treeNode.deptName,
                                    shortName: treeNode.shortName,
                                    fullName: treeNode.fullName,
                                    deptAuthType: treeNode.deptAuthType,
                                    deptType: treeNode.deptType,
                                    deptInnerId: treeNode.deptInnerId
                                });
                            });
                        }
                    }
                }
            };
            Page.ajaxPostQuiet("department/tree.do", {parentDeptId: attrs.edDept}, function (response) {
                if (response.data.length > 0) {
                    response.data[0].open = true;// 根节点展开
                }
                $(response.data).each(function(index, dept) {
                    dept.iconSkin = 'dept';
                });
                var ztree = $.fn.zTree.init($("#" + scope.treeId), setting, response.data);
                ztree.expandAll(true);
            });
        }

    };
}]);
// 用户所属部门
App.directive('edUserDept', ['Page', function (Page) {
    return {

        restrict: 'A',
        scope: {
            model : '@ngModel',
            userId: '=edUserDept'
        },
        require: 'ngModel',

        controller: ['$scope', '$element', function ($scope, $element) {
        }],
        
	    link: function (scope, elem, attrs, ngModelController) {
	    	scope.$watch('userId', function(newValue, oldValue) {
                if (newValue != oldValue
                    || (newValue && newValue == oldValue && (elem.children().length == 0 || (elem.children().length == 1 && elem.children()[0].text == '')))) {
                    Page.ajaxGetQuiet('department/ofUser.do', {userId: newValue}, function(resp) {
                        var options = '';
                        $(resp.data).each(function(index, dept) {
                        	if (index == 0) {
                        		ngModelController.$setViewValue(dept.deptId);// 只包含id
                                ngModelController.$render();
                        	}
                            options += '<option value="' + dept.deptId + '">' + dept.deptName + '</option>';
                        });
                        $(elem).html(options);
                    });
                }
            });
	    }

    };
}]);

